package com.javainterface.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.javainterface.entity.OrgInfo;
import com.javainterface.entity.UserInfo;
import com.javainterface.service.DataTransmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单点系统对接数据的接口类
 */
@Controller
@RequestMapping("/DataTransmission")
public class DataTransmissionController {

    @Autowired
    DataTransmissionService dataTransmissionService;


    @PostMapping("/userTransmission")
    @ResponseBody
    public net.sf.json.JSONObject userDataTransmission(@RequestBody String json) {
        Map<String, Object> map = new HashMap<>();
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            List<UserInfo> userInfoList = JSON.parseArray(jsonObject.getString("data"), UserInfo.class);
            for (int i = 0; i < userInfoList.size(); i++) {
                dataTransmissionService.userInfoTransmission(
                        userInfoList.get(i).getOrg(), userInfoList.get(i).getUserFlag(), userInfoList.get(i).getUserAccount(),
                        userInfoList.get(i).getUserPwd(), userInfoList.get(i).getUserName(), userInfoList.get(i).getSex(),
                        userInfoList.get(i).getPhoneNumber(), userInfoList.get(i).getTelPhone(), userInfoList.get(i).getEmail(),
                        userInfoList.get(i).getJob(), userInfoList.get(i).getIdentityNumber(), userInfoList.get(i).getUserIndex(),
                        userInfoList.get(i).getUserStatus(), userInfoList.get(i).getBusinessLabel(), userInfoList.get(i).getFirstLoginFlag(),
                        userInfoList.get(i).getBannedFlag(), userInfoList.get(i).getDeleteFlag(), userInfoList.get(i).getCreateTime(), userInfoList.get(i).getUpdateTime()
                );
                map.put("code", 200);
                map.put("message", "success");
            }
        } catch (Exception e) {
            map.put("code", 201);
            map.put("message", "error");
            e.printStackTrace();
        }
        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(map);
        return jsonObject;
    }

    @PostMapping("/orgTransmission")
    @ResponseBody
    public net.sf.json.JSONObject orgDataTransmission(@RequestBody String json) {
        Map<String, Object> map = new HashMap<>();
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            List<OrgInfo> orgInfoList = JSON.parseArray(jsonObject.getString("data"), OrgInfo.class);
            for (int i = 0; i < orgInfoList.size(); i++) {
                dataTransmissionService.orgInfoTransmission(
                        orgInfoList.get(i).getOrgName(), orgInfoList.get(i).getOrgNameLite(), orgInfoList.get(i).getOrgNameParent(),
                        orgInfoList.get(i).getOrgNameEnglish(),orgInfoList.get(i).getOrgNameEnglishLite(), orgInfoList.get(i).getOrgType(), orgInfoList.get(i).getOrgLevel(),
                        orgInfoList.get(i).getSocialCode(), orgInfoList.get(i).getOrgIndex(), orgInfoList.get(i).getOrgStatus(),
                        orgInfoList.get(i).getOrgId(), orgInfoList.get(i).getOrgPath(), orgInfoList.get(i).getBannedFlag(),
                        orgInfoList.get(i).getDeleteFlag(), orgInfoList.get(i).getCreateTime(), orgInfoList.get(i).getUpdateTime()
                );
                map.put("code", 200);
                map.put("message", "success");
            }
        } catch (Exception e) {
            map.put("code", 201);
            map.put("message", "error");
            e.printStackTrace();
        }
        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(map);
        return jsonObject;
    }


}
